/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.runtime.module.xml.transformers.xml;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.core.api.transformer.Transformer;
import org.mule.runtime.core.util.IOUtils;
import org.mule.runtime.module.xml.transformer.DomDocumentToXml;
import org.mule.runtime.module.xml.transformer.XmlToDomDocument;
import org.mule.runtime.module.xml.util.XMLTestUtils;
import org.mule.runtime.module.xml.util.XMLUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.dom4j.DocumentHelper;
import org.dom4j.io.DOMWriter;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class DomXmlTransformersTestCase extends AbstractXmlTransformerTestCase {

  private String srcData;
  private Document resultData;

  @Override
  protected void doSetUp() throws Exception {
    srcData = IOUtils.getResourceAsString("cdcatalog.xml", getClass());
    org.dom4j.Document dom4jDoc = DocumentHelper.parseText(srcData);
    resultData = new DOMWriter().write(dom4jDoc);
  }

  @Override
  public Transformer getTransformer() throws Exception {
    XmlToDomDocument trans = createObject(XmlToDomDocument.class);
    trans.setReturnDataType(DataType.fromType(org.w3c.dom.Document.class));
    return trans;
  }

  @Override
  public Transformer getRoundTripTransformer() throws Exception {
    DomDocumentToXml trans = createObject(DomDocumentToXml.class);
    trans.setReturnDataType(DataType.STRING);
    return trans;
  }

  @Override
  public Object getTestData() {
    return srcData;
  }

  @Override
  public Object getResultData() {
    return resultData;
  }

  @Test
  public void testTransformXMLStreamReader() throws Exception {
    Object expectedResult = getResultData();
    assertNotNull(expectedResult);

    XmlToDomDocument transformer = (XmlToDomDocument) getTransformer();

    InputStream is = IOUtils.getResourceAsStream("cdcatalog.xml", XMLTestUtils.class);
    XMLStreamReader sr = XMLUtils.toXMLStreamReader(transformer.getXMLInputFactory(), is);

    Object result = transformer.transform(sr);
    writeXml((Node) result, System.out);
    assertNotNull(result);
    assertTrue("expected: " + expectedResult + "\nresult: " + result, compareResults(expectedResult, result));
  }

  public static void writeXml(Node n, OutputStream os) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    // identity
    javax.xml.transform.Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(n), new StreamResult(os));
  }

  @Test
  public void testAllXmlMessageTypes() throws Exception {
    List list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
    Iterator it = list.iterator();

    Object expectedResult = getResultData();
    assertNotNull(expectedResult);

    Object msg, result;
    while (it.hasNext()) {
      msg = it.next();
      result = getTransformer().transform(msg);
      assertNotNull(result);
      assertTrue("Test failed for message type: " + msg.getClass(), compareResults(expectedResult, result));
    }
  }

}
